import tkinter as tk
import random
import math


def IsPrime(number):  # 判断是否为素数
    if number == 1:
        return False
    for j in range(2, int(math.sqrt(number)) + 1):  # 从2到number的算术平方根迭代
        if number % j == 0:  # 判断j是否为number的因数
            return False
    return True

def click():
    #初始化清空
    # txt1.delete(0,tk.END)
    # txt2.delete(0,tk.END)
    hqwb=txt1.get()
    print(hqwb)
    a = list(map(int, hqwb.split()))
    n = len(a)
    for i in range(0, n - 1):
        k = i 
        if IsPrime(a[k]):
            flag = True
        else:
            flag = False
        for j in range(i, n):
            if IsPrime(a[j]):
                if a[j]<a[k] or flag == False:
                    k = j
                    flag = True
        if k != i:
            a[k], a[i] = a[i], a[k]
        if not flag:
            continue
    print(a)
    txt2.insert(0,a)
    #for i in range(n-1,-1,-1):
    #    txt2.insert(0,','+str(a[i]))

win=tk.Tk()
win.title('产生随机个数的正整数排序，素数在前升序，非素数在后')
win.geometry ('600x150')
win.resizable(200,200)
btn = tk.Button(win,text='开始排序',command=click)
lb1 = tk.Label(win,text='请以空格为间隔符输入任意长度的正整数：')
lb2 = tk.Label(win,text='排序结果：')
lb3 = tk.Label(win,text='排序结果：')
txt1 = tk.Entry(win)
txt2 = tk.Entry(win)
txt1.grid(row=2,column=2)
txt2.grid(row=4,column=2)
lb1.grid(row=2,column=1)
lb2.grid(row=4,column=1)
btn.grid(row=1,column=1,columnspan=2,pady=10)
win.mainloop()
          
